// pages/user/order/order.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 'all',
    orderPage: 1,
    orderType: '',
    orderList: [],
    itemPrice: 0.00
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    if (options.active != '') {
      that.setData({
        active: options.active
      })
    } else {
      that.setData({
        active: 'all'
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getOrderList(this.data.orderPage, this.data.active)
  },

  /**
   * 上拉
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.getOrderList(this.data.orderPage, this.data.active)
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },

  /**
   * 下拉
   */
  onReachBottom: function() {
    let that = this,
      page = that.data.orderPage
    page = page + 1
    that.setData({
      orderPage: page
    })
    that.getOrderList(that.data.orderPage, that.data.active)
  },

  /**
   * tab
   */
  tab: function(e) {
    let active = e.currentTarget.dataset.index,
      page = 1
    this.setData({
      active: active
    })
    this.getOrderList(page, this.data.active)
  },

  getOrderList: function(page, order_type) {
    let that = this,
      list = that.data.orderList,
      orderList = [],
      data, k
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/order/list?access_token=' + accessToken,
        data: {
          page: page,
          order_type: order_type
        },
        success: res => {
          data = res.data.data
          if (res.data.status === 1) {
            if (page == 1) {
              that.setData({
                orderList: data
              })
            } else {
              if (data.length != 0) {
                for (k in data) {
                  list.push(data[k])
                }
                that.setData({
                  orderList: list
                })
              }
            }
          }
          wx.hideLoading()
        }
      })
    })
  },

  /**
   * 确认提货
   */
  cfmOrder: function(e) {
    let that = this,
      oid = e.currentTarget.dataset.oid
    wx.showModal({
      title: '提示',
      content: '是否确认已提货',
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            mask: true,
            title: app.globalData.loadText,
          })
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/order/confirm-take?access_token=' + accessToken + '&order_id=' + oid,
              success: res => {
                if (res.data.status === 1) {
                  wx.showToast({
                    icon: 'none',
                    title: '确认收货成功',
                    duration: 3000,
                    success: res => {
                      that.getOrderList(1, that.data.active)
                    }
                  })
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 3000,
                    success: res => {
                      wx.hideLoading()
                    }
                  })
                }
              }
            })
          })
        }
      }
    })
  },

  /**
   * 取消订单
   */
  cancelOrder: function(e) {
    let that = this,
      oid = e.currentTarget.dataset.oid
    wx.showModal({
      title: '提示',
      content: '是否取消订单',
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            mask: true,
            title: app.globalData.loadText,
          })
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/order/cancel?access_token=' + accessToken + '&order_id=' + oid,
              success: res => {
                if (res.data.status === 1) {
                  wx.showToast({
                    icon: 'none',
                    title: '取消订单成功',
                    success: res => {
                      that.getOrderList(1, that.data.active)
                      wx.hideLoading()
                    }
                  })
                }
              }
            })
          })
        }
      }
    })
  },

  /**
   * 继续付款
   */
  goOnPay: function(e) {
    let that = this,
      data
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/wxpay/unified-order?access_token=' + accessToken,
        data: {
          order_sn: e.currentTarget.dataset.osn
        },
        success: res => {
          data = res.data.data
          if (res.data.status === 1) {
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: 'MD5',
              paySign: data.paySign,
              success: res => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: '购买成功',
                })
                that.getOrderList(that.data.orderPage, that.data.active)
              },
              fail: err => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: '交易取消',
                })
              }
            })
          }
        }
      })
    })
  }
})